<?php

/**

 * Created by PhpStorm.

 * User: win7

 * Date: 2018/7/7 0007

 * Time: 14:52

 */



namespace Api\Controller;



use Think\Controller;



define("TOKEN", "xiangtws");//定义识别码

$wechatObj = new EventController();//实例化wechatCallbackapiTest类



if(!isset($_GET["echostr"])){

    $wechatObj->responseMsg();

}else{

    $wechatObj->valid();

}



class EventController extends Controller

{

    public function valid()

    {

        $echoStr = $_GET["echostr"];



        if($this->checkSignature()){

            echo $echoStr;

            exit;

        }

    }

    public function responseMsg()//执行接收器方法

    {



        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];



        if (!empty($postStr)){



            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

            $RX_TYPE = trim($postObj->MsgType);

            switch($RX_TYPE){

                case "event":

                    $result = $this->receiveEvent($postObj);

                    break;

            }

            echo $result;

        }else{

            echo "";

            exit;

        }

    }

    private function receiveEvent($object){

        $content = "";

        switch ($object->Event){

            case "subscribe":

                //关注

                if(session($object->FromUserName)!=1){

                    session($object->FromUserName,1);

                    $this->sendmsg($object->FromUserName);

                }//end if

                $content = "欢迎加入【乡土味士】，为了感谢武汉社区居民的支持，【乡土味士】武汉粉丝试吃会终于来了！，<a href='http://www.xiangtws.com/Wx/Foretaste/index'>点击链接</a>即可参与偏远山村0元试吃活动，助力偏远山村。欢迎加入【乡土味士】，我们一直在等你！";



//乡土味士，帮助社区对接乡村。在这里，大家既能吃好又能助力乡村发展：拼团乡村农产品，社区店铺二次分拣为你的拼团订单提供终端保障，还有品牌礼品免费送。以后，不出社区也可吃到乡间美味";//这里是向关注者发送的提示信息



                break;

            case "unsubscribe":

                session($object->FromUserName,0);

                //取消关注

                $content = "";

                break;

        }



        $result = $this->transmitText($object,$content);



        return $result;

    }



    private function transmitText($object,$content){

        $textTpl = "<xml>

       <ToUserName><![CDATA[%s]]></ToUserName>

       <FromUserName><![CDATA[%s]]></FromUserName>

       <CreateTime>%s</CreateTime>

       <MsgType><![CDATA[text]]></MsgType>

       <Content><![CDATA[%s]]></Content>

       <FuncFlag>0</FuncFlag>

       </xml>";

        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;



    }

    private function checkSignature()

    {

        $signature = $_GET["signature"];

        $timestamp = $_GET["timestamp"];

        $nonce = $_GET["nonce"];



        $token = TOKEN;

        $tmpArr = array($token, $timestamp, $nonce);

        sort($tmpArr, SORT_STRING);

        $tmpStr = implode( $tmpArr );

        $tmpStr = sha1( $tmpStr );

        //开源代码最模板zuimoban.com

        if( $tmpStr == $signature ){

            return true;

        }else{

            return false;

        }

    }

    public function sendmsg($openid){

        $cache=S(array(

                'type'=>'memcache',

                'host'=>'127.0.0.1',

                'port'=>'11211',

                'prefix'=>'focus',

                'expire'=>86400)

        );

        $one=M('member')->where("openid like '".$openid."'")->getField('id');

        //推送商品信息

        $ca_title="title_".$one;

        //$cache->$ca_title=$title;

        $ca_url="url_".$one;

        //$cache->$ca_url=$url;

        $title=$cache->$ca_title;

        $url=$cache->$ca_url;

        if($title!=''){

            D("Wx/SendMsg")->focusmsg($one,$title,$url);

            $cache->$ca_title='';

            $cache->$ca_url='';

        }





    }//end fun



}//end class